package com.neu.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neu.entity.Department;
import com.neu.service.DepartmentService;
import com.neu.util.JsonResult;
import com.neu.util.ResultCode;



@CrossOrigin
@RestController("departmentController")
@RequestMapping("/his/department")
public class DepartmentController {
	@Autowired
	private DepartmentService departmentService;
	
	/**
	 * 
	 * @param pageNum 接收请求携带的当前页号
	 * @param pageSize 接收请求中携带的分页的字段数
	 * @param keywords 收到的关键字参数
	 * @return json输出到前端
	 */
	
	@RequestMapping("/get_dept")
	public JsonResult<List<Department>> getDeptMore(@RequestParam("pageNum") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize,
			@RequestParam("keywords")String keywords){
		List<Department> list = departmentService.getDepartmentList(pageNum, pageSize, keywords);
		Integer count = departmentService.getDepartmentCount(keywords);
		JsonResult<List<Department>> result = new JsonResult<List<Department>>(ResultCode.SUCCESS,list,count);
		return result;
	}
	
	/**
	 * 通过更多关键字实现的查询方法
	 * @param pageNum 接收请求携带的当前页号
	 * @param pageSize 接收请求中携带的分页的字段数
	 * @param keywords 收到的关键字参数
	 * @param keywords02 收到的关键字参数2
	 * @param keywords03 收到的关键字参数3
	 * @return
	 */
	
	@RequestMapping("/get_dept_more")
	public JsonResult<List<Department>> getDeptMore(@RequestParam("pageNum") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize,
			@RequestParam("keywords")String keywords,
			@RequestParam("keywords02")String keywords02,
			@RequestParam("keywords03")String keywords03){
		List<Department> list = departmentService.getDepartmentList02(pageNum, pageSize, keywords, keywords02, keywords03);
		return new JsonResult<List<Department>>(ResultCode.SUCCESS,list,departmentService.getDepartmentCount02(keywords, keywords02, keywords03));
	}
	
	/**
	 * 删除单个部门
	 * @param id 接收到请求中的部门编号
	 * @return
	 */
	@RequestMapping("/delete_dept")
	public JsonResult<Integer> deleteDept(@RequestParam("id") Integer id){
			Integer row = departmentService.deleteDepartmentById(id);
			if(row != 1 ) {
				return new JsonResult<Integer>(ResultCode.DEL_ERROR,row);
			}

			return new JsonResult<Integer>(ResultCode.SUCCESS,row);
	}
	
	/**
	 * 通过接收请求中body携带的数组来批量删除部门表中的字段
	 * @param ids 收到请求携带的数组
	 * @return 返回json data中携带的是删除的字段条数
	 */
	@RequestMapping(value="/delete_dept_more",method=RequestMethod.POST)
	JsonResult<Integer> deleteDeptMore(@RequestBody Integer[] ids){
			Integer rows =departmentService.deleteDepartmentByIds(ids);
			JsonResult<Integer> result = new JsonResult<>(ResultCode.SUCCESS,rows);
			return result;
	} 	
	
	
	@RequestMapping(value="/add_dept",method=RequestMethod.POST)
	JsonResult<Integer> addDept(Department dept){
		Integer rows = departmentService.addDepartment(dept);
		if(rows != 1) {
			return new JsonResult<Integer>(ResultCode.INSERT_ERROR,rows);
		}
		return new JsonResult<Integer>(ResultCode.SUCCESS,rows);
	}
}
